package com.ning.run.common.utils;

import java.util.Random;

/**
 * @Description 随机数相关操作
 * @Author wood
 * @Date 2021/3/2 - 10:32
 */
public class RandomUtil {
    private static final String SOURCE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";


    /**
     * 生成指定位数的随机数
     *
     * @param digit 数字的位数
     * @return
     */
    public static String getRandomNum(int digit) {
        Random random = new Random();
        String result = "";
        for (int i = 0; i < digit; i++) {
            result += random.nextInt(10);
        }
        return result;
    }

    /**
     * 生成指定位数的随机字符串
     * @param digit 字符串的位数
     * @return
     */
    public static String getRandomStr(int digit) {
        Random random = new Random();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < digit; i++) {
            int number = random.nextInt(62);
            sb.append(SOURCE.charAt(number));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(getRandomStr(6));
    }

}
